//
//  PopoverAnimator.swift
//  XNWeiBo_Swift
//
//  Created by xunan on 16/6/22.
//  Copyright © 2016年 xunan. All rights reserved.
//

import UIKit

let XMGPopoverAnimatorWillShow = "XMGPopoverAnimatorWillShow"
let XMGPopoverAnimatorWillDismess = "XMGPopoverAnimatorWillDismess"

class PopoverAnimator: NSObject, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning
{
    
    // 定义属性 保存大小
    var presentFrame = CGRectZero
    
    // 记录是否展开
    var isPresent: Bool? = false
    
    // 实现代理方法 谁负责专场动画
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController?, sourceViewController source: UIViewController) -> UIPresentationController? {
        let pc = PopoverPresentationController(presentedViewController: presented, presentingViewController: presenting)
        pc.presentFrame = presentFrame
        return pc
    }
    
    // 怎么出现
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        // 通知控制器展开
        NSNotificationCenter.defaultCenter().postNotificationName(XMGPopoverAnimatorWillShow, object: nil)
        return self
    }
    
    // 怎么销毁
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        // 通知控制器关闭
        NSNotificationCenter.defaultCenter().postNotificationName(XMGPopoverAnimatorWillDismess, object: nil)
        return self
    }
    
    //MARK: - 转场protocol
    
    // 返回动画时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    
    // 如何动画
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        //        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewKey)
        //        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewKey)
        
        if isPresent == true { // 展开
            // 1.拿到
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
            toView?.transform = CGAffineTransformMakeScale(1.0, 0.0)
            
            // 设置锚点
            toView?.layer.anchorPoint = CGPoint(x: 0.5, y: 0.0)
            
            transitionContext.containerView().addSubview(toView!)
            
            // 2.动画
            UIView .animateWithDuration(transitionDuration(transitionContext), animations: {
                toView?.transform = CGAffineTransformIdentity
            }) { (_) in
                transitionContext.completeTransition(true)
            }
            
        } else { // 消失
            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
            
            UIView .animateWithDuration(transitionDuration(transitionContext), animations: {
                fromView?.transform = CGAffineTransformMakeScale(1.0, 0.00001)
                }, completion: { (_) in
                    transitionContext.completeTransition(true)
            })
            
        }
        
    }
    
}
